package com.syncy.process;

import java.util.ArrayList;

import com.syncy.process.exceptions.InitializationException;
import com.syncy.process.exceptions.WrongStateException;

public class Process implements Runnable {
	
	private ProcessType type = null;
	private ProcessConcurrencyType concurrency = null;
	private ProcessStatus status = ProcessStatus.DISABLED;
	private ArrayList<Step> steps;
	private int currentStep = 0;
	private boolean requestToStop = false;
	
	public Thread runningThread = null;
	
	private ProcessRunningOptions options = null;
	
	public Process (String uid, ArrayList<Step> steps, ProcessRunningOptions options) {
		
		if(type == null || concurrency == null || steps == null){
			throw new InitializationException(this, type, concurrency, steps);
		}
		
		this.type = type;
		this.concurrency = concurrency;
		this.steps = steps;
		
		runningThread = new Thread(this, uid);	
	}
	
	public void setState(ProcessStateAction action) {
		
		try {
		
			
			
		} catch ( WrongStateException ex) {
			
		} catch ( RuntimeException ex) {
			
		}
		
		
	}

	@Override
	public void run() {
		
		if(this.type == ProcessType.CONTINUOS_TYPE) {
			
	    try {
	    	long in = this.options.getSleepInterval();
	    	while (!requestToStop) {
	            	//Do something
	            	for(Step s : steps){
	            		boolean success = s.execute();
	            		if(!s.onFailContinue()) {
	            			break;
	            		}
	            	}
					if(!requestToStop){
	    			//Then Sleep a while
						Thread.sleep(in);
					}
	    		}
	    	} catch (InterruptedException e){
	    		
	    	}
		}

		if(this.type == ProcessType.CRON_TYPE) {
			
			
		}
		
		
	}
	
	public void stop() {
		this.requestToStop = true;
	}
	
	public Thread getRunningThread() {
		return runningThread;
	}
	
}
